﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aspose.BarCode;
using ECare.BUS.Data;
//

namespace ECare.BUS.Workers
{
    public class ResourcesInfoReportBuilder : BackgroundWorker
    {
         public ResourcesInfoReportBuilder()
        {
            this.DoWork += InInfoReportBuilder_DoWork;
        }
        public ResoucesInfo CurrentResourceInfo { get; set; }

        void InInfoReportBuilder_DoWork(object sender, DoWorkEventArgs e)
        {
            string currentDir = System.Environment.CurrentDirectory + @"\print\";

            if (!Directory.Exists(currentDir))
                Directory.CreateDirectory(currentDir);

            string filename = DateTime.Now.ToString("yyyyMMddhhmmss");

            string desitionhtml = currentDir + filename + ".htm";
            string barcodepng = currentDir + filename + ".png";


            //生成barcode...
            var barcodeBuilder = new BarCodeBuilder
            {
                CodeText = CurrentResourceInfo.ResourceCode,
                SymbologyType = Symbology.Code128,
                Resolution = new Resolution(100f, 200f, ResolutionMode.Customized)
            };

            barcodeBuilder.Save(barcodepng, ImageFormat.Png);


            string html = string.Empty;
            html = @"<html>";
            //header
            html = html + @"<header><title>物料信息单</title></header>"; //the header is the scan converts Id
            html = html + System.Environment.NewLine;
            //body
            html = html + "<body>";
            html = html + System.Environment.NewLine;
            //show the LOGO and title
            html = html + "<TABLE align='center' cellSpacing=0 cellPadding=0 width=\"100%\" border=1 style=\"font-family: 'Calibri'; font-size: 12pt;\">";
            html = html + System.Environment.NewLine;

            html = html + "<TR>";
            html = html + "<td><strong>物料编码</strong></td>";
            html = html + string.Format("<td>{0}</td>", CurrentResourceInfo.ResourceCode);
            html = html + "<td><strong>物料名称</strong></td>";
            html = html + string.Format("<td>{0}</td>", CurrentResourceInfo.ResourceName);

            html = html + "<td><strong>物料单位</strong></td>";
            html = html + string.Format("<td>{0}</td>", CurrentResourceInfo.ResourceUnit);
            html = html + "</TR>";

            html = html + "<TR>";
            html = html + "<td><strong>物料TS</strong></td>";
            html = html + string.Format("<td>{0}</td>", CurrentResourceInfo.ResourceTs);

            html = html + "<td><strong>地区</strong></td>";
            html = html + string.Format("<td>{0}</td>",CurrentResourceInfo.ResourceRegion);

            html = html + "<td><strong>有效期</strong></td>";
            html = html + string.Format("<td>{0} 公斤</td>", CurrentResourceInfo.ResouceValidate);
            html = html + "</TR>";

            html = html + "<TR>";
            html = html + string.Format("<TD colspan='6' align='center'><image src='{0}'/></TD>", barcodepng);
            html = html + "</TR>";

            html = html + "</table>";
            html = html + "</body>"; html = html + System.Environment.NewLine;
            html = html + "</html>";

            File.WriteAllText(desitionhtml, html, Encoding.GetEncoding("gb2312"));
            e.Result = desitionhtml;

        }
    }
}
